package com.mengzhilan.controller;

import com.mengzhilan.base.Model;
import com.mengzhilan.context.MZLApplicationContext;
import com.mengzhilan.mapper.TestMapper;
import org.xlp.auto.create.exception.AutoCreatorDataLoseException;
import org.xlp.auto.create.util.AutoCreatorContextUtils;
import org.xlp.bean.annotation.AutoFill;
import org.xlp.db.ddl.OracleDBTableCreator;
import org.xlp.db.ddl.TableCreator;
import org.xlp.db.utils.XLPDBUtil;
import org.xlp.mvc.annotation.*;
import org.xlp.mvc.context.ApplicationContextUtils;
import org.xlp.mvc.enumeration.RequestMethodType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Create by xlp on 2021/12/11
 */
@CBController
public class TestController {
    private static final ThreadLocal<Integer> THREAD_LOCAL = new ThreadLocal<>();

    @AutoFill
    private TestMapper testMapper;


    public void setTestMapper(TestMapper testMapper) {
        this.testMapper = testMapper;
    }


    @RequestMapping(value = "/test", method = RequestMethodType.GET)
    public String test(HttpServletResponse response, HttpServletRequest request,
                       @RequestParam(required = true) String id){
        String url = request.getScheme()+ "://" + request.getServerName()
                + ":" + request.getServerPort();
        String contextPath = request.getContextPath();
        if (contextPath.startsWith("/") && contextPath.length() == 1) {
            return url + contextPath;
        }
        if (!contextPath.startsWith("/")) {
            url += "/";
        }
        url += contextPath;
        if (!contextPath.endsWith("/")) {
            url += "/";
        }
        System.out.println(url);
        System.out.println(XLPDBUtil.getDatabaseProductName());

        return "{\"id\":" + id + "}";
    }

    @ResponseCharset("gbk")
    @RequestMapping(value = "/test2", method = RequestMethodType.GET)
    public void test2(HttpServletResponse response,  String id1) throws IOException {
        System.out.println(response);
        System.out.println(((MZLApplicationContext) ApplicationContextUtils.getContext()).getClassesByAnnotation(Model.class));
        response.getWriter().write(id1+"1好");
    }

    @ResponseCharset("utf-8")
    @RequestMapping(value = "/test123{id}{name}", method = RequestMethodType.GET)
    public void test3(HttpServletResponse response, @PathVariable("id") String id2,
                      @PathVariable("name") String name) throws IOException {
        System.out.println(response);
        response.getWriter().write(id2+"4" + name);
    }

    @ResponseCharset("utf-8")
    @RequestMapping(value = "/test123{id}23{name}", method = RequestMethodType.GET)
    public void test2(HttpServletResponse response, @PathVariable("id") String id2,
                      @PathVariable("name") String name) throws IOException {
        System.out.println(response);
        response.getWriter().write(id2+"2" + name);
    }

    @ResponseCharset("utf-8")
    @RequestMapping(value = "/createTable", method = RequestMethodType.GET)
    public String createTable(HttpServletResponse response) throws IOException, SQLException {
         //TableCreator tableCreator = new MYSqlTableCreator(XLPDBUtil.getConnection());
         TableCreator tableCreator = new OracleDBTableCreator(XLPDBUtil.getConnection());
         //tableCreator.createTableByEntityClass(FormItemBaseInfo.class);
         return "表创建成功";
    }

    @RequestMapping(value = "/createJava", method = RequestMethodType.GET)
    public String createJava(HttpServletResponse response) throws IOException, AutoCreatorDataLoseException {
        //E:\ideaworkspace\mengzhilan\src\main\java
        AutoCreatorContextUtils.autoCreate("G:\\20200325\\mengzhilan-serve\\src\\main\\java", "com.mengzhilan", "FormItemBaseInfo",
                "model", true, false, false);
        return "实体相关java文件创建成功";
    }

    @RequestMapping(value = "/testLocal", method = RequestMethodType.GET)
    public String testLocal() {
        Integer integer = THREAD_LOCAL.get();
        if (integer == null){
            THREAD_LOCAL.set(12);
        }
        System.out.println(Thread.currentThread().hashCode());
        return integer + "";
    }
}
